<?php
namespace app\decorate\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\decorate\model\AdminArea as area_model;

class AdminArea extends AdminRest{
    protected $model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new area_model();
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:地区列表
     */
    public function areaList(){
        $data  = $this->model->areaSelect(['uniacid'=>$this->_uniacid]);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:添加地区
     */
    public function areaAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $info = $this->model->areaInfo(['title'=>$input['title'],'pid'=>$input['pid'],'uniacid'=>$this->_uniacid]);
        if($info){
            return $this->error('地址已存在');
        }
        $res   = $this->model->areaAdd($input);
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-10-14 15:23
     * @return void
     * descption:获取第一级地区
     */
    public function areaOneSelect(){
        $data  = $this->model->areaSelect(['uniacid'=>$this->_uniacid],2);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:59
     * @return \think\Response
     * descption:编辑|回显地区
     */
    public function areaUpdate(){
        $input = $this->_input;
        $data  = $this->model->areaInfo(['id'=>$input['id']]);
        if(count($input)>1){
            $info = $this->model->areaInfo(['title'=>$input['title'],'pid'=>$input['pid'],'uniacid'=>$this->_uniacid]);
            if($info&&$info['id'] != $input['id']){
                return $this->error('地址已存在');
            }
            $child = $this->model->areaInfo(['pid'=>$data['id']]);
            if(!empty($child)&&$input['pid']!=0){
               return $this->error('该地区已有下级，只能选择顶级区域');
            }
            $data = $this->model->areaUpdate(['id'=>$input['id']],$input);
        }
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 11:44
     * @return \think\Response
     * descption:区域状态编辑
     */
    public function statusUpdate(){
        $input = $this->_input;
        if($input['status']==0){
            $id = $this->model->getSon([$input['id']]);
        }else{
            $id = [$input['id']];
        }
        $dis[]= ['id','in',$id];
        $data = $this->model->areaUpdate($dis,['status'=>$input['status']]);
        return $this->success($data);
    }








}
